package com.zjw.xm.xmorder.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zjw.xm.entity.CrBook;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zjw.xm.entity.CrCategory;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author zjw1
 * @since 2022-08-03
 */
@Mapper
public interface CrBookMapper extends BaseMapper<CrBook> {

    @Select("select * from cr_book where name like '%年%' or name like '%志%'")
    List<CrBook> selectByFznj();


    @Select(" SELECT DISTINCT c.* from cr_category a  join cr_category b  join cr_book c \n" +
            "    on a.id =b.pid  and a.id=c.category1   \n" +
            "    WHERE a.id =#{id} or b.id= #{id} limit #{page},#{size} ")
    List<Map<String,Object>> selectBookPage(@Param("id") int id ,
                                           @Param("page") int page ,
                                           @Param("size") int size);

    @Select(" SELECT DISTINCT count(*) cnt from cr_category a  join cr_category b  join cr_book c \n" +
            "           on a.id =b.pid  and a.id=c.category1   \n" +
            "           WHERE a.id =#{id} or b.id= #{id}")
    Map<String,Object> countById(int id);

    @Select(" SELECT DISTINCT c.* from cr_category a  join cr_category b  join cr_book c \n" +
            "    on a.id =b.pid  and a.id=c.category1   \n" +
            "    WHERE a.name like concat('%',#{name},'%')  or b.name like concat('%',#{name},'%') or c.name like concat('%',#{name},'%') limit #{page},#{size} ")
    List<Map<String,Object>> search(@Param("name") String name,
                                    @Param("page") int page,
                                    @Param("size") int size);

    @Select(" SELECT DISTINCT count(*) cnt from cr_category a  join cr_category b  join cr_book c \n" +
            "    on a.id =b.pid  and a.id=c.category1   \n" +
            "    WHERE a.name like concat('%',#{name},'%') or b.name like concat('%',#{name},'%') or c.name like concat('%',#{name},'%') ")
    Map<String,Object> countByName(String name);


    //后台管理数据查询
    @Select({
            "<script>" ,
            "SELECT * FROM cr_book  where 1=1 ",
            "<if test='name != null  '>",
            " and name like '%${name}%' ",
            "</if>",
            " limit #{page},#{size}",
            "</script>"
    })
    List<CrBook> selectBackBooks(@Param("name") String name,
                                 @Param("page") int page,
                                 @Param("size") int size);

    @Select({
            "<script>" ,
            "SELECT count(*) cnt FROM cr_book where 1=1",
            "<if test='name != null '>",
            " and name like '%${name}%'",
            "</if>",
            "</script>"
    })
    Map<String,Object> countBackBooks( String name);

    //查询分类一
    @Select(" SELECT id,name from cr_category WHERE pid is null; ")
    List<Map<String,Object>> queryCate1();

    //根据分类一查询分类二
    @Select(" SELECT DISTINCT b.id,b.name from cr_category a JOIN cr_category b ON \n" +
            " a.id=b.pid WHERE a.id=#{id} ")
    List<Map<String,Object>> queryCate2(Integer id);

    @Select("SELECT b.id,b.name from cr_category a JOIN cr_category b ON\n" +
            " a.id=b.pid where a.pid is not null and a.id=#{id} ")
    List<Map<String,Object>> queryCate3(Integer id);

}
